//
//  BrowseViewController.m
//  InteriorDesign
//
//  Created by lanou on 15/12/16.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BrowseViewController.h"
#import "MainHeaderScrollView.h"
#import "MainLabelModel.h"
#import "MainTableViewCell.h"
#import "MainDetailViewController.h"
#import "UIScrollView+MJRefresh.h"

@interface BrowseViewController ()

//滚动视图
@property (nonatomic, retain)MainHeaderScrollView *mainHeader;
//数组存储首页展示数据
@property (nonatomic, retain)NSMutableArray *mainArray;
//pageControl
@property (nonatomic, retain)UIPageControl *pageControl;

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, assign)NSInteger number;

@end

@implementation BrowseViewController

//懒加载
- (NSMutableArray *)mainArray
{
    if (!_mainArray) {
        _mainArray = [[NSMutableArray alloc]init];
    }
    return _mainArray;
}

#pragma mark-
#pragma mark-------------------解析数据----
//解析首页滚动视图
- (void)jsonMainHeaderScrollView{
    [LORequestManger GET:kMainScroll success:^(id response) {
        NSMutableArray *imageArray = [NSMutableArray array];
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *pic in dic[@"data"][@"featured_banners2"][@"banners"]) {
            [imageArray addObject:pic[@"pic"]];
            //NSLog(@"%@",imageArray);
        }
        self.mainHeader = [[MainHeaderScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 260) target:self action:@selector(mainHeaderImageTouch) imageArray:imageArray timer:1 selectro:@selector(autoImage)];
        self.tableView.tableHeaderView = _mainHeader;
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 150, 300, 15)];
        self.pageControl.backgroundColor = [UIColor redColor];
        self.pageControl.numberOfPages = 4;
        self.pageControl.currentPage = 0;
        self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
        [self.mainHeader addSubview:self.pageControl];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

//解析表视图
- (void)jsonMainTableViewCell{

    
    NSString *urlString = [NSString stringWithFormat:@"http://api.meilijia.com/app/featured_page3?page=%ld",self.number];
    [LORequestManger GET:urlString success:^(id response) {
        NSDictionary *rootDic = (NSDictionary *)response;
        for (NSDictionary *itemsDic in rootDic[@"data"][@"featured_items"][@"items"]) {
            
            MainLabelModel *mainLabelModel = [MainLabelModel shareJsonWithDictonary:itemsDic];
            if (mainLabelModel.author_face != nil) {
            [self.mainArray addObject:mainLabelModel];
            }
        }
        // NSLog(@"%@",self.mainArray);
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.number = 1;
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.title = @"HOMELIST";
    [self jsonMainHeaderScrollView];
    [self jsonMainTableViewCell];
    
    //注册cell
    [self.tableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"mainTablecell"];
    
    
    [self.tableView addSubview:self.mainHeader];
    [self.view addSubview:self.tableView];

    __weak BrowseViewController *mainView = self;
    [mainView.tableView addHeaderWithCallback:^{
        NSLog(@"上拉刷新");
        [mainView jsonMainHeaderScrollView];
        [mainView.tableView headerEndRefreshing];
    }];
    
    [mainView.tableView addFooterWithCallback:^{
        NSLog(@"上拉刷新");
        mainView.number += 1;
        [mainView jsonMainTableViewCell];
        [mainView.tableView footerEndRefreshing];
    }];
    
}


#pragma mark-
#pragma mark----------------实现滚动视图方法
//点击事件
- (void)mainHeaderImageTouch{
    NSInteger index = self.mainHeader.contentOffset.x / kWidth;
    NSInteger maxIndex = self.mainHeader.contentSize.width / kWidth;
    if (index == maxIndex - 1) {
        index = 0;
    }
    if (index != 0) {
        index -= 1;
    }
    NSLog(@"%ld",index);
}
//自动滚动
- (void)autoImage{
    [self scrollViewDidCircularly];
    
    //获取当前的scrollView的contentoffset
    CGPoint newPoint = _mainHeader.contentOffset;
    //将x的值添加kwidth
    newPoint.x += kWidth;
    //重新设置scrollView的contentOffset
    [_mainHeader setContentOffset:newPoint animated:YES];
}

- (void)scrollViewDidCircularly{
    if (self.mainHeader.contentOffset.x == self.mainHeader.contentSize.width - kWidth) {
        [self.mainHeader setContentOffset:CGPointMake(kWidth, 0) animated:NO];
        //判断我们当前的scrollView的contentoffset的X值为我们最后一张图片的时候，将我们的scrollView的contentOffset设置为我们提前定义好的第一张图片位置，且将动画效果关闭造成视觉差
    }
}


//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.mainArray.count;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 260;
}

//加载单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MainLabelModel *mainModel = self.mainArray[indexPath.row];
    MainTableViewCell *mainCell = [tableView dequeueReusableCellWithIdentifier:@"mainTablecell" forIndexPath:indexPath];
    mainCell.mainModel = mainModel;
    return mainCell;
}

//点击后进入
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    MainDetailViewController *mainDetail = [[MainDetailViewController alloc]init];
    mainDetail.mailLabel = _mainArray[indexPath.row];
    [self presentViewController:mainDetail animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
